<?php
namespace Common\Model;

use Think\Model\RelationModel;

class OrderDetailModel extends RelationModel
{
    protected $_link = array(
        'Product' => array(
            'mapping_type' => self::BELONGS_TO,
            'mapping_name' => 'product',
            'foreign_key' => 'product_id',//关联id
        ),
        'Order' => array(
            'mapping_type' => self::BELONGS_TO,
            'mapping_name' => 'order',
            'foreign_key' => 'order_id',
        ),
        'File' => array(
            'mapping_type' => self::BELONGS_TO,
            'mapping_name' => 'file',
            'foreign_key' => 'file_id',//关联id
            'as_fields' => 'savename:savename,savepath:savepath',
        ),
    );

    public function getOrderDetailList($condition = array(), $relation = false, $order = "id desc", $p = 0, $num = 0, $limit = 0)
    {
        $list = $this->where($condition);
        if ($relation) {
            $list = $list->relation(true);
        }
        if ($p && $num) {
            $list = $list->page($p . ',' . $num . '');
        }
        if ($limit) {
            $list = $list->limit($limit);
        }

        $list = $list->order($order)->select();

        return $list;
    }

    public function updateOrderDetail($id , $data)
    {
        $this->where(array("id" => $id))->save($data);
    }
}